package com.stadsetjari_hi.Fragments;

import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

import com.stadsetjari_hi.R;

public class Settings_Fragment extends Fragment implements OnClickListener {

  	private SharedPreferences preferences;
  	private SharedPreferences.Editor editor;
  	private String[] nums = {"1","2","3","4","5","10","15","20","25","30","40","50","60"};
  	private String[] deviceGroup = {"1","2","3","4"};
  	private NumberPicker np, np2;
  	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
		
        View v = inflater.inflate(R.layout.settings_frag, container, false);
        
        getActivity();
		preferences = getActivity().getSharedPreferences("my_preferences", Context.MODE_APPEND);
        editor = preferences.edit();
        
        np = (NumberPicker) v.findViewById(R.id.np);
        np.setMaxValue(nums.length);
        np.setMinValue(1);
        np.setWrapSelectorWheel(true);
        np.setDisplayedValues(nums);
        
        np2 = (NumberPicker) v.findViewById(R.id.np2);
        np2.setMaxValue(deviceGroup.length);
        np2.setMinValue(1);
        np2.setWrapSelectorWheel(true);
        np2.setDisplayedValues(deviceGroup);

       	int savedInterval = preferences.getInt("interval", 6);
       	np.setValue(savedInterval);
       	
       	int savedDeviceGroup = preferences.getInt("deviceGroup", 1);
       	np2.setValue(savedDeviceGroup);
       	
       	editor.commit();

        Button button = (Button) v.findViewById(R.id.Settings);
        
        button.setOnClickListener(this);
        return v;
    }

	@Override
	public void onClick(View v) {
		switch(v.getId())
  		{
  			case R.id.Settings:
  				
  				try
  				{
  					String intervalString = nums[np.getValue()-1];
  					String deviceGroupString = deviceGroup[np2.getValue()-1];
	  				Toast.makeText(getActivity(), intervalString + " minutes, " + deviceGroupString, 5).show();

	  				editor.putInt("interval", np.getValue());
	  				editor.putInt("intervalTransmit", Integer.parseInt(nums[np.getValue()-1]));
	  				editor.putInt("deviceGroup", np2.getValue());
	  				editor.putString("deviceGroupTransmit", deviceGroup[np2.getValue()-1]);
	  				editor.commit();
  				}
  				catch(Exception e)
  				{
  					Log.d("finnur ekki", "textabox");
  				}

  				break;	
  		}
	}
} 